<?php

class Core_Model_Promotion_Collection_Concessions extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('promotion/concessions');
        $this->from('{t}promotion_concessions', 'a')
                ->innerJoin('{t}promotion_concessions_category', 'b', 'b.id', 'a.category_id')
                ->field('a.*')
                ->field('b.name', 'category_name');
    }

    public function joinCustomer(){
         $this->innerJoin('{t}promotion_concessions_poi', 'c', 'c.concessions_id', 'a.id')
                ->field('c.poi_id','poi_id')
                ->innerJoin('{t}foundation_poi', 'd', 'd.id', 'poi_id')
                ->field('d.address','address')
                ->field('d.business_phone','business_phone')
                ->field('d.business_hours','business_hours');
         return $this;
    }

    public function joinSubject(){
        $this->innerJoin('{t}subject_concessions','d','d.concessions_id','a.id')
                    ->field('subject_id')
                    ->field('is_recommend');
        return $this;
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                return $this->eq('a.id', ':id:i')->addParameter('id', $arg);
            case 'name':
                return $this->where()->like('a.name', ':name:s')->closest()->addParameter('name', '%' . $arg . '%');
            case 'starttime':
                return $this->lteq('a.start_time', ':startTime:s')->addParameter('startTime', $arg);
            case 'endtime':
                return $this->where()->group()->gteq('a.end_time', ':endTime')->orNext()->isNull('a.end_time')->closest()
                        ->addParameter('endTime', $arg);
            case 'status':
                return $this->eq('a.status', ":status:s")->addParameter('status', $arg);
            case 'categoryname':
                return $this->like('b.name', ':categoryName:s')->addParameter('categoryName', "%$arg%");
            case 'categoryid':
                return $this->eq('a.category_id', ':category_id:i')->addParameter('category_id', "$arg");
            case 'ownerid':
                return $this->eq('a.owner_id', ':owner_id:i')->addParameter('owner_id', "$arg");
            case 'tag':
                $tagNames = explode(' ', $arg);
                for ($i = 0; $i < count($tagNames); $i++) {
                    if ($i > 0) {
                        $this->orNext();
                    }
                    $this->like('a.tag', "'%{$tagNames[$i]}%'");
                }
                break;
            case 'ids':
                return $this->contain('a.id', ':ids:i')->addParameter('ids', $arg);
            case 'poiid':
                return $this->contain('c.poi_id', ':poiId:s')->addParameter('poiId', $arg);
        }
        return $this;
    }
     public function sortBy($name, $dir) {
        switch ($name) {
            case 'hot':
                return $dir == 'asc' ? $this->asc("a.is_hot") : $this->desc("a.is_hot");
            case 'new':
                return $dir == 'asc' ? $this->asc("a.is_new") : $this->desc("a.is_new");
            case 'top':
                return $dir == 'asc' ? $this->asc("a.is_top") : $this->desc("a.is_top");
            case 'modified_time':
                return $dir == 'asc' ? $this->asc("a.modified_time") : $this->desc("a.modified_time");
            case 'click_count':
                return $dir == 'asc' ? $this->asc("a.click_count") : $this->desc("a.click_count");
        }
        return parent::sortBy($name, $dir);
    }
}